# -*- coding: utf-8 -*-
# @Author : 左手
# @File : 01-数据保存.py
# @Software: PyCharm
# @Date : 2024/10/12 19:31

import csv
"""
图片，视频，音频 -- 保存在某个文件夹里面
html网页源码 -- 保存的.html文件
txt/word/表格文件

csv模块: 内置模块
对表格文件进行读取/写入
[(第一行数据),(第二行数据),(第三行数据)]

[{第一行数据},{第二行数据},{第三行数据}]
"""

# data = [('肖申克的救赎', '9.7', '希望让人自由'),('七龙珠', '孙悟空', '贝吉塔'),('阿甘正传', '9.5', '一部美国近现代史')]
#
# # 1.设置标题  列表嵌套元祖  2.元祖
# head = ('标题','评分','引言')
# #
# # # 2.保存数据  # with open  底层 能够自动帮你关闭文件  但是如果是open方法 最后要利用close()才能进行关闭
# with open('D:\pycharm_wj\demo.csv', 'a', encoding='utf-8-sig', newline='')as f:
#     # 3.创建csv写入对象
#     writer = csv.writer(f) # 传入的是文件对象
#     # 4.写入表头  writerow(): 写入一行数据  writerows(): 一次性写入多行
#     writer.writerow(head)
#     # 5.写入数据
#     writer.writerows(data)


# data = [
#     {'title':'肖申克的救赎', 'star': '9.7', 'quote': '希望让人自由'},
#     {'title':'肖申克的救赎', 'star': '9.7', 'quote': '希望让人自由'},
#     {'title':'肖申克的救赎', 'star': '9.7', 'quote': '希望让人自由'}
# ]
#
# # 设置表头，表头一定要跟字典的key保持一致
# head = ('title', 'star', 'quote')
#
# # 数据保存 w: 写入(重新写入会覆盖掉之前的)  a: 追加  r:是读取
# with open('text1.csv', 'w', encoding='utf-8-sig', newline='')as f:
#     # 创建csv对象   fieldnames=head 设置表头信息
#     writer = csv.DictWriter(f, fieldnames=head)
#     # 写入标头: 单独方法
#     writer.writeheader()
#     # 写入数据
#     writer.writerows(data)



# 读取数据
# with open('text.csv', 'r', encoding='utf-8-sig')as f:
    # 读取数据
    # reader = csv.reader(f) # 传入文件对象
    # print(next(reader)) # 返回的是一个文件生成器
    # print(next(reader)) # 返回的是一个文件生成器
    # print(next(reader)) # 返回的是一个文件生成器
    # print(next(reader)) # 返回的是一个文件生成器
    # for i in reader:
    #     print(i)  # 返回的数据类型:list



# 读取数据
# with open('text1.csv', 'r', encoding='utf-8-sig')as f:
#     reader = csv.DictReader(f) # 传入的是文件对象
#     # print(next(reader)) # 返回的是一个文件生成器
#     for i in reader:
#         print(i)


import jsonpath
import requests

url = 'https://api-c.liepin.com/api/com.liepin.searchfront4c.pc-search-job'

dic = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
    'x-client-type': 'web',
    'x-fscp-bi-stat': '{"location": "https://www.liepin.com/zhaopin/?city=410&dq=410&pubTime=&currentPage=0&pageSize=40&key=python&suggestTag=&workYearCode=&compId=&compName=&compTag=&industry=&salary=&jobKind=&compScale=&compKind=&compStage=&eduLevel=&otherCity=&sfrom=search_job_pc&ckId=hwmufbahh4eacxrzevqwbxwfh36ui9d9&scene=input&skId=hwmufbahh4eacxrzevqwbxwfh36ui9d9&fkId=hwmufbahh4eacxrzevqwbxwfh36ui9d9&suggestId="}',
    'x-fscp-std-info': '{"client_id": "40108"}',
    'x-fscp-trace-id': 'b86ee2da-2570-45a0-928f-f576df413e7b',
    'x-fscp-version': '1.1',
    'x-requested-with': 'XMLHttpRequest',
    'x-xsrf-token': 'o0ogm-nSR2aGVx6-ZKNvHw'
}

lst = []
for page in range(0, 5):
    data = {"data": {
        "mainSearchPcConditionForm": {"city": "410", "dq": "410", "pubTime": "", "currentPage": page, "pageSize": 40,
                                      "key": "python", "suggestTag": "", "workYearCode": "", "compId": "",
                                      "compName": "",
                                      "compTag": "", "industry": "", "salary": "", "jobKind": "", "compScale": "",
                                      "compKind": "", "compStage": "", "eduLevel": ""},
        "passThroughForm": {"scene": "input", "skId": "hwmufbahh4eacxrzevqwbxwfh36ui9d9",
                            "fkId": "hwmufbahh4eacxrzevqwbxwfh36ui9d9", "ckId": "21bcx4yjgwasvl0fufb6lbxdb7vyikj8"}}}

    # 发请求,获取响应
    res = requests.post(url, json=data, headers=dic)
    result = res.json()  # 字典
    # print(result)
    """
    确定url是正确的，代码也没有问题 -- 数据少了或者不正确(反爬)

    反爬参数如果全部放进去 会有参数陷阱
    """
    acc = result['data']['data']['jobCardList']  # lst1是获取的数据
    # print(lst)
    #
    for i in acc:
        dic1 = {}
        dic1['title'] = i['job']['title']
        dic1['salary'] = i['job']['salary']
        dic1['compName'] = i['comp']['compName']
        lst.append(dic1)
        # print(title, salary, compName)
# print(lst)  # 打印的是空列表

# 设置表头，表头一定要跟字典的key保持一致
head = ('title', 'salary', 'compName')

# 数据保存 w: 写入(重新写入会覆盖掉之前的)  a: 追加  r:是读取
with open('text1.csv', 'w', encoding='utf-8-sig', newline='')as f:
    # 创建csv对象   fieldnames=head 设置表头信息
    writer = csv.DictWriter(f, fieldnames=head)
    # 写入标头: 单独方法
    writer.writeheader()
    # 写入数据
    writer.writerows(lst)